package com.SeleniumTestsFirefox;

import com.thoughtworks.selenium.Selenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
import com.thoughtworks.selenium.SeleneseTestBase;

public class RegisterAccountTestCases extends SeleneseTestBase {
	@Before
	public void setUp() throws Exception {
		WebDriver driver = new FirefoxDriver();
		String baseUrl = "http://localhost:8080/Website/";
		selenium = new WebDriverBackedSelenium(driver, baseUrl);
	}

	// Created by Steven Chow
	// Use Case: Register Account
	// Scenario 1: Successful Registration
	// Test Case 1: Valid Information
	@Test
	public void testRegisterAccountSC1TC1() throws Exception {
		// Go to Website
		selenium.open("");

		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium.isElementPresent("class=tile double bg-color-blue"));

		// STEP 1: Click on "New Customer Button"
		selenium.click("//a[2]/div/div[2]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Registration page will load
		assertEquals("Register Customer", selenium.getTitle());

		// STEP 2: Enter valid username, password, first, lastname, email, and
		// phone
		selenium.type("name=username", "BlueBear");
		selenium.type("name=password", "pass");
		selenium.type("name=firstname", "Blue");
		selenium.type("name=lastname", "Bear");
		selenium.type("name=email", "BlueBear@hotmail.com");
		selenium.type("name=phone", "987");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check for a Registration Success Message
		assertTrue(selenium.isTextPresent("Hi, Blue! Registration success!"));
		// Go Back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
		// Click on "Login Button"
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		assertEquals("Customer Login", selenium.getTitle());
		// Enter in username and password you just registered for
		selenium.type("name=username", "BlueBear");
		selenium.type("name=password", "pass");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		assertEquals("Customer Portal", selenium.getTitle());
		assertEquals("BlueBear", selenium.getText("class=first-name"));
		// EXPECTED RESULTS 3: Check if the Customer can view the Customer
		// Portal Page
		// Logout
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 3: Check if the Customer has logged out are
		// redirected back to the main page. Their username should disappear
		// from the top right
		assertEquals("PlaneSimple", selenium.getTitle());
		assertEquals("", selenium.getText("class=first-name"));
	}

	// Created by Justin Ng
	// Use Case: Register Account
	// Scenario 1: Successful Registration
	// Test Case 2: Valid Information Phone Variant #2
	@Test
	public void testRegisterAccountSC1TC2() throws Exception {
		// Go to Website
		selenium.open("");

		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium.isElementPresent("class=tile double bg-color-blue"));

		// STEP 1: Click on "New Customer Button"
		selenium.click("//a[2]/div/div[2]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Registration page will load
		assertEquals("Register Customer", selenium.getTitle());

		// STEP 2: Enter valid username, password, first, lastname, email, and
		// phone
		selenium.type("name=username", "MadBear");
		selenium.type("name=password", "pass");
		selenium.type("name=firstname", "Blue");
		selenium.type("name=lastname", "Bear");
		selenium.type("name=email", "BlueBear@hotmail.com");
		selenium.type("name=phone", "123-456-7890");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check for a Registration Success Message
		assertTrue(selenium.isTextPresent("Hi, Blue! Registration success!"));
		// Go Back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
		// Click on "Login Button"
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		assertEquals("Customer Login", selenium.getTitle());
		// Enter in username and password you just registered for
		selenium.type("name=username", "BlueBear");
		selenium.type("name=password", "pass");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		assertEquals("Customer Portal", selenium.getTitle());
		assertEquals("BlueBear", selenium.getText("class=first-name"));
		// EXPECTED RESULTS 3: Check if the Customer can view the Customer
		// Portal Page
		// Logout
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 3: Check if the Customer has logged out are
		// redirected back to the main page. Their username should disappear
		// from the top right
		assertEquals("PlaneSimple", selenium.getTitle());
		assertEquals("", selenium.getText("class=first-name"));
	}

	// Created by Justin Ng
	// Use Case: Register Account
	// Scenario 1: Successful Registration
	// Test Case 3: Username, Password, Phone filled in
	@Test
	public void testRegisterAccountSC1TC3() throws Exception {
		// Go to Website
		selenium.open("");

		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium.isElementPresent("class=tile double bg-color-blue"));

		// STEP 1: Click on "New Customer Button"
		selenium.click("//a[2]/div/div[2]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Registration page will load
		assertEquals("Register Customer", selenium.getTitle());

		// STEP 2: Enter valid username, password, phone
		selenium.type("name=username", "GreenBear");
		selenium.type("name=password", "pass");
		selenium.type("name=phone", "123");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check for a Registration Success Message
		assertTrue(selenium.isTextPresent("Hi, ! Registration success!"));
		// Go Back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
		// Click on "Login Button"
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		assertEquals("Customer Login", selenium.getTitle());
		// Enter in username and password you just registered for
		selenium.type("name=username", "GreenBear");
		selenium.type("name=password", "pass");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		assertEquals("Customer Portal", selenium.getTitle());
		assertEquals("GreenBear", selenium.getText("class=first-name"));
		// EXPECTED RESULTS 3: Check if the Customer can view the Customer
		// Portal Page
		// Logout
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 3: Check if the Customer has logged out are
		// redirected back to the main page. Their username should disappear
		// from the top right
		assertEquals("PlaneSimple", selenium.getTitle());
		assertEquals("", selenium.getText("class=first-name"));
	}

	// Created by Justin Ng
	// Use Case: Register Account
	// Scenario 1: Unsuccessful Registration
	// Test Case 1: Missing Information
	@Test
	public void testRegisterAccountSC2TC1() throws Exception {
		// Go to Website
		selenium.open("");

		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium.isElementPresent("class=tile double bg-color-blue"));

		// STEP 1: Click on "New Customer Button"
		selenium.click("//a[2]/div/div[2]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Registration page will load
		assertEquals("Register Customer", selenium.getTitle());

		// STEP 2: Enter valid password, first, lastname, email, and phone.
		// Leave out username.
		selenium.type("name=password", "pass");
		selenium.type("name=firstname", "Blue");
		selenium.type("name=lastname", "Bear");
		selenium.type("name=email", "BlueBear@hotmail.com");
		selenium.type("name=phone", "987");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check for a Registration Failure Message
		assertTrue(selenium.isTextPresent("Missing fields"));
		// Go Back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());

		// STEP 3: Repeat Account Creation
		selenium.click("//a[2]/div/div[2]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 3: Check if the Customer Registration page will load
		assertEquals("Register Customer", selenium.getTitle());

		// STEP 4: Enter valid username, first, lastname, email, and phone.
		// Leave out password.
		selenium.type("name=username", "GayBear");
		selenium.type("name=firstname", "Blue");
		selenium.type("name=lastname", "Bear");
		selenium.type("name=email", "BlueBear@hotmail.com");
		selenium.type("name=phone", "987");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 4: Check for a Registration Failure Message
		assertTrue(selenium.isTextPresent("Missing fields"));
		// Go Back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());

		// STEP 5: Repeat Account Creation
		selenium.click("//a[2]/div/div[2]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 5: Check if the Customer Registration page will load
		assertEquals("Register Customer", selenium.getTitle());

		// STEP 6: Enter valid username, password, first, lastname, and email.
		// Leave out phone.
		selenium.type("name=username", "BrownBear");
		selenium.type("name=password", "pass");
		selenium.type("name=firstname", "Blue");
		selenium.type("name=lastname", "Bear");
		selenium.type("name=email", "BlueBear@hotmail.com");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS : Check for a Registration Failure Message
		assertTrue(selenium.isTextPresent("Missing fields"));
		// Go Back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
	}

	// Created by Steven Chow
	// Use Case: Register Account
	// Scenario 2: UnSuccessful Registration
	// Test Case 2: Invalid Length

	@Test
	public void testRegisterAccountSC2TC2() throws Exception {
		// Go to Website
		selenium.open("");

		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium.isElementPresent("class=tile double bg-color-blue"));

		// STEP 1: Click on "New Customer Button"
		selenium.click("//a[2]/div/div[2]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Registration page will load
		assertEquals("Register Customer", selenium.getTitle());

		// STEP 2: Enter a username with an Invalid Length
		selenium.type("name=username",
				"5555555555555555555555ddfadddddddddddddddddddddddddddddddddddd");
		selenium.type("name=password", "fg");
		selenium.type("name=firstname", "asdf");
		selenium.type("name=lastname", "sfd");
		selenium.type("name=email", "BlueBear@hotmail.com");
		selenium.type("name=phone", "987");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check for a Registration Success Message
		assertTrue(selenium.isTextPresent("Invalid username."));
		// Go Back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());

		// Repeat with invalid password
		selenium.click("//a[2]/div/div[2]");
		selenium.waitForPageToLoad("30000");
		assertEquals("Register Customer", selenium.getTitle());
		// STEP 3: Enter a password with an Invalid Length
		selenium.type("name=username", "fjhy6tu");
		selenium.type("name=password",
				"3444444444444444444444444aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
		selenium.type("name=firstname", "asdf");
		selenium.type("name=lastname", "adf");
		selenium.type("name=email", "BlueBear@hotmail.com");
		selenium.type("name=phone", "987");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 4: Check for a Registration Success Message
		assertTrue(selenium.isTextPresent("Invalid password."));
		// Go Back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());

		// Repeat with invalid First Name
		selenium.click("//a[2]/div/div[2]");
		selenium.waitForPageToLoad("30000");
		assertEquals("Register Customer", selenium.getTitle());
		// STEP 4: Enter a 1st Name with an Invalid Length
		selenium.type("name=username", "stystytry");
		selenium.type("name=password", "adfadfafd");
		selenium.type("name=firstname",
				"dssssssssssssssssssssssssssssssss344444444444444444444444444444444adfdadf");
		selenium.type("name=lastname", "adf");
		selenium.type("name=email", "BlueBear@hotmail.com");
		selenium.type("name=phone", "987");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 5: Check for a Registration Success Message
		assertTrue(selenium.isTextPresent("Invalid First Name."));
		// Go Back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());

		// Repeat with invalid Last Name
		selenium.click("//a[2]/div/div[2]");
		selenium.waitForPageToLoad("30000");
		assertEquals("Register Customer", selenium.getTitle());
		// STEP 5: Enter a Last Name with an Invalid Length
		selenium.type("name=username", "");
		selenium.type("name=password", "adsf");
		selenium.type("name=firstname", "asdf");
		selenium.type("name=lastname",
				"adsfffffffffffffffffffff3434344444444444444444444444adfdfadfsdadfdfadfdf");
		selenium.type("name=email", "BlueBear@hotmail.com");
		selenium.type("name=phone", "987");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 6: Check for a Registration Success Message
		assertTrue(selenium.isTextPresent("Invalid Last Name."));
		// Go Back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
		// Repeat with invalid Email
		selenium.click("//a[2]/div/div[2]");
		selenium.waitForPageToLoad("30000");
		assertEquals("Register Customer", selenium.getTitle());
		// STEP 6:: Enter a E-mail with an Invalid Length
		selenium.type("name=username", "adf");
		selenium.type("name=password", "asdf");
		selenium.type("name=firstname", "asdf");
		selenium.type("name=lastname", "adf");
		selenium.type(
				"name=email",
				"BlueBear@hotmail.com3444444444444444444444444asdffffffffffffffffffffffffffffffffffffffffffffffffffffdfdfdfdfdfdf");
		selenium.type("name=phone", "987");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 7: Check for a Registration Success Message
		assertTrue(selenium.isTextPresent("Invalid E-mail."));
		// Go Back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
	}

	// Created by Steven Chow
	// Use Case: Register Account
	// Scenario 2: UnSuccessful Registration
	// Test Case 1: Invalid Phone Number
	@Test
	public void testRegisterAccountSC2TC3() throws Exception {
		// Go to Website
		selenium.open("");

		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium.isElementPresent("class=tile double bg-color-blue"));

		// STEP 1: Click on "New Customer Button"
		selenium.click("//a[2]/div/div[2]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Registration page will load
		assertEquals("Register Customer", selenium.getTitle());

		// STEP 2: Enter valid username, password, first, lastname, email, and
		// invalid phone
		selenium.type("name=username", "GayBear");
		selenium.type("name=password", "pass");
		selenium.type("name=firstname", "Blue");
		selenium.type("name=lastname", "Bear");
		selenium.type("name=email", "BlueBear@hotmail.com");
		selenium.type("name=phone", "dfdfdfdfdfdfdf");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check for an Error Message
		assertTrue(selenium.isTextPresent("invalid phone number"));
		// Go Back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());

		// Step 3: Input another invalid Phone Number. This time a super long
		// integer
		selenium.click("//a[2]/div/div[2]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 3: Check if the Customer Registration page will load
		assertEquals("Register Customer", selenium.getTitle());

		// Enter valid username, password, first, lastname, email, and invalid
		// phone
		selenium.type("name=username", "BlueBear");
		selenium.type("name=password", "pass");
		selenium.type("name=firstname", "Blue");
		selenium.type("name=lastname", "Bear");
		selenium.type("name=email", "BlueBear@hotmail.com");
		selenium.type("name=phone",
				"333333333333333333333333333333333333333333333333333333333333454");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// EXPECTED RESULTS 4: Check for an Error Message
		assertTrue(selenium.isTextPresent("invalid phone number"));
		// Go Back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
	}

	// Created by Steven Chow
	// Use Case: Register Account
	// Scenario 2: UnSuccessful Registration
	// Test Case 4: User already exists
	@Test
	public void testRegisterAccountSC2TC4() throws Exception {
		// Go to Website
		selenium.open("");

		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium.isElementPresent("class=tile double bg-color-blue"));

		// STEP 1: Click on "New Customer Button"
		selenium.click("//a[2]/div/div[2]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Registration page will load
		assertEquals("Register Customer", selenium.getTitle());

		// STEP 2: Enter a username with an Invalid Length
		selenium.type("name=username", "User1");
		selenium.type("name=password", "fg");
		selenium.type("name=firstname", "asdf");
		selenium.type("name=lastname", "sfd");
		selenium.type("name=email", "BlueBear@hotmail.com");
		selenium.type("name=phone", "987");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check for a Registration Success Message
		assertTrue(selenium
				.isTextPresent("Username exists. Please pick another one!"));
		// Go Back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
	}

	// Created by Steven Chow
	// Use Case: Register Account
	// Scenario 3: Cancelled Registration
	// Test Case 1: Hit Back
	@Test
	public void testRegisterAccountSC3TC1() throws Exception {
		// Go to Website
		selenium.open("");
		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium.isElementPresent("class=tile double bg-color-blue"));

		// STEP 1: Click on "New Customer Button"
		selenium.click("//a[2]/div");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Registration page will load
		assertEquals("Register Customer", selenium.getTitle());

		// STEP 2: Enter valid username, password, first, lastname, email, and
		// invalid phone
		selenium.type("name=username", "Bluebear");
		selenium.type("name=password", "bluebear");
		selenium.type("name=firstname", "fdsafdsf");
		selenium.type("name=lastname", "sdafa");
		selenium.type("name=email", "sdfadfadf");
		selenium.type("name=phone", "dfasfds");
		// Hit the Back Button
		selenium.click("//a[contains(@href, '../index.html')]");
		selenium.waitForPageToLoad("30000");
		// Try to login
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		selenium.type("name=username", "Bluebear");
		selenium.type("name=password", "bluebear");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check if you get an error message
		assertTrue(selenium.isTextPresent("Login"));
		assertTrue(selenium.isTextPresent(""));

		// Go back to main page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
	}

	@After
	public void tearDown() throws Exception {
		selenium.stop();
	}
}
